﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace RenameEpisodes
{
    enum Mode { Pattern, File };
    class Program
    {
        
        static void Main(string[] args)
        {
            string path = null, pattern= "", fileName=null;
            Mode mode = Mode.Pattern;
             if (args.Length < 2 || args.Length > 3)
            {
                ShowHelp();
                return;
            }
            else
            {
                if (args.Length == 2)
                {
                    path = args[0];
                    pattern = args[1];
                }
                else if (args.Length == 3)
                {
                    bool commandFound = false;
                    foreach (string s in args)
                    {
                        if (!commandFound)
                        {
                            if (s.Equals("-names", StringComparison.CurrentCultureIgnoreCase))
                            {
                                commandFound = true;
                            }
                            else
                            {
                                path = s;
                            }
                        }
                        else
                        {
                            fileName = s;
                            mode = Mode.File;
                        }

                    }

                }
            }
            if (mode== Mode.Pattern && !pattern.Contains("{0}"))
            {
                ShowHelp();
                return;
            }
            if (mode == Mode.File && !File.Exists(fileName))
            {
                ShowHelp();
            }
            RenameEpisodesJob job = new RenameEpisodesJob(mode, path, pattern, fileName);
            job.TestMode = true;
            job.Start();

            Console.WriteLine();
            Console.WriteLine("Proceed with the operation Y/N?");
            string answer= Console.ReadLine();
            if (answer.Equals("Y", StringComparison.CurrentCultureIgnoreCase))
            {
                job.TestMode = false;
                job.Start();
            }

        }

        private static void ShowHelp()
        {
            Console.WriteLine("Usage: RenameEpisodes <PATH> [<PATTERN> | -names <FILEWITHNAMES>]");
            Console.WriteLine("<PATTERN>: Must contain the string {0}, that will be replaced by the episode id (SxEE)");
            Console.WriteLine("<FILEWITHNAMES>: Must contain the names of the episodes, one line per episode");
            Console.WriteLine(@"Example: RenameEpisodes D:\Series\BigBangTheory ""{0} BigBangTheory""  will rename all the movie files in D:\Series\BigBangTheory to the pattern SxEE BigBangTheory, matching each episode.");
            Console.WriteLine(@"Example: RenameEpisodes D:\Series\TrueBlood-2 -names TrueBlood-season2.txt  will rename all the movie files in D:\Series\TrueBlood-2 to the corresponding name in TrueBlood-season2.txt.");

        }
    }
}
